﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Cn.Edwon.Web.Support;
using Cn.Cellula.Biz.Port;
using Cn.Edwon.Spring;
using System.Reflection;
using Cn.Cellula.Biz;
using Cn.Cellula.Model;
using Cn.Edwon.Web;
using System.Collections.Generic;
using System.Text;
using Cn.Edwon.Web.Validation;
using Cn.Cellula.Model.Product;
using Cn.Cellula.Client;
namespace Cn.Cellula.Website.Admin.Product {
    public partial class EditSaleInfo : PageSupport {
        IProductBiz biz = BizFactory.Get<IProductBiz>();
        ProductEditBiz peb = new ProductEditBiz();
        protected void Page_Load(object sender, EventArgs e) {
            if (!Page.IsPostBack) {
                InitForm();
            }
        }
        private void InitForm() {
            TProductEO peo = null;
            try {
                peo = biz.Load<TProductEO>(Request.QueryString["id"]);
                this.hid_ProductKey.Value = peo.ProductId;
                this.lab_ProductName.Text = peo.ProductName;
                this.hid_CategoryKey.Value = peo.CategoryId;
                this.lab_CategoryName.Text = BizFactory.Get<ICategoryBiz>().LoadCategoryPath(this.OwnerKey, CodeTypeDef.Category.Product, peo.CategoryId, " / ");

            } catch {
                SiteInfo.Send(Page, "错误", "没有指定有效的产品");
                this.btn_Save.Enabled = false;
                return;
            }
            try {
                TProductSaleEO ps = biz.Load<TProductSaleEO>(peo.ProductId);
                this.txt_CostPrice.Text = ps.CostPrice.ToString();
                this.txt_GroupPrice.Text = ps.GroupPrice + "";
                this.txt_PreferentialPrice.Text = ps.PreferentialPrice + "";
                this.txt_ProductNum.Text = ps.ProductCount + "";
                this.txt_RetailPrice.Text = ps.RetailPrice + "";
                this.txt_TransCost.Text = ps.TransCost + "";
                this.hid_HasSaleInfo.Value = "1";
            } catch {
                this.hid_HasSaleInfo.Value = "0";
            }
        }

        protected void btn_Save_Click(object sender, EventArgs e) {
            Boolean bk = QuickValidator.Execute(Request, this.txt_CostPrice.Parent);
            if (!bk) {
                SiteInfo.Send(Page, TxtDef.ERROR, QuickValidator.Message);
                return;
            }
            TProductSaleEO ps = new TProductSaleEO();
            try {
                ps.CostPrice = Convert.ToDecimal(this.txt_CostPrice.Text);
            } catch {
                ps.CostPrice = 0;
            }
            try {
                ps.GroupPrice = Convert.ToDecimal(this.txt_GroupPrice.Text);
            } catch {
                ps.GroupPrice = 0;
            }
            try {
                ps.PreferentialPrice = Convert.ToDecimal(this.txt_PreferentialPrice.Text);
            } catch {
                ps.PreferentialPrice = 0;
            }
            try {
                ps.ProductCount = Convert.ToInt32(this.txt_ProductNum.Text);
            } catch {
                ps.ProductCount = 0;
            }
            try {
                ps.RetailPrice = Convert.ToDecimal(this.txt_RetailPrice.Text);
            } catch {
                ps.RetailPrice = 0;
            }
            try {
                ps.TransCost = Convert.ToDecimal(this.txt_TransCost.Text);
            } catch {
                ps.TransCost = 0;
            }
            ps.UpdateDate = DateTime.Now;
            ps.ProductKey = this.hid_ProductKey.Value;
            try {
                if (this.hid_HasSaleInfo.Value == "0") {
                    biz.Save<TProductSaleEO>(ps);
                } else {
                    biz.Update<TProductSaleEO>(ps);
                }
                Response.Redirect("List.aspx");
            } catch (Exception ex) {
                log.Error(ex.Message);
                SiteInfo.Send(Page, TxtDef.ERROR, "保存数据时发生错误");
                return;
            }
        }
    }
}
